package site.suremotoo.demo.delaypay.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liulx
 * @description TODO
 * @date 2020-12-28 20:52
 */
@Configuration
public class MqConfig {

    /**
     * 下单
     *
     * @return
     */
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(QueueConstant.ORDER_EXCHANGE_NAME, true, true);
    }

    @Bean
    public Queue orderQueue() {
        return QueueBuilder.durable(QueueConstant.ORDER_QUEUE_NAME).build();
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(QueueConstant.ORDER_ROUTING_KEY);
    }


    /**
     * 支付
     */

    @Bean
    public DirectExchange payExchange() {
        return new DirectExchange(QueueConstant.PAY_EXCHANGE_NAME, true, true);
    }

    @Bean
    public Queue payQueue() {
        Map<String, Object> args = new HashMap<>(8);
        // 10s 消息到期
        args.put("x-message-ttl", 10000);
        //配置死信队列属性
        args.put("x-dead-letter-exchange", QueueConstant.DLX_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", QueueConstant.DLX_ROUTING_KEY);
        return new Queue(QueueConstant.PAY_QUEUE_NAME, true, false, false, args);
        //return QueueBuilder.durable(QueueConstant.PAY_QUEUE_NAME).withArguments(args).build();
    }

    @Bean
    public Binding payBinding() {
        return BindingBuilder.bind(payQueue()).to(payExchange()).with(QueueConstant.PAY_ROUTING_KEY);
    }


    /**
     * 死信队列
     */
    @Bean
    public DirectExchange dlxPayExchange() {
        return new DirectExchange(QueueConstant.DLX_EXCHANGE_NAME, true, true);
    }

    @Bean
    public Queue dlxPayQueue() {
        return QueueBuilder.durable(QueueConstant.DLX_QUEUE_NAME).build();
    }

    @Bean
    public Binding dlxPayBinding() {
        return BindingBuilder.bind(dlxPayQueue()).to(dlxPayExchange()).with(QueueConstant.DLX_ROUTING_KEY);
    }

}
